package com.wencst.generateJPA.builder.json;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.util.ResourceUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wencst.generateJPA.bean.TableBean;
import com.wencst.generateJPA.bean.TransferBean;
import com.wencst.generateJPA.builder.IBuilder;
import com.wencst.generateJPA.config.CoreContext;
import com.wencst.security.bean.Menu;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class AbstractJsonBuilder implements IBuilder {

	protected CoreContext coreContext;
	private TransferBean tb;
	
	public AbstractJsonBuilder(CoreContext coreContext) {
		this.coreContext = coreContext;
		tb = new TransferBean();
		TableBean table = coreContext.getTb();
		tb.setColumns(table.getColumns());
		tb.setComment(table.getComment());
		tb.setJavaBeanName(table.getJavaBeanName());
		tb.setTableName(table.getTableName());
		tb.setAuthor(coreContext.getAuthorName());
		tb.setThisPackage(coreContext.getParentPackage());
	}
	@Override
	public void build() {
        try {
			File file = ResourceUtils.getFile("classpath:templates/freemaker/json/menus.json");
            File newFile = getNewFile();
            if (!newFile.exists()) {
            	FileUtils.copyFile(file, newFile);
            }
            addDataInto(tb);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        } finally {
            log.info("complete adding menus file —— {}", coreContext.getTb().getTableName());;
        }
	}

	@Override
	public String getDirName() {
		return coreContext.getJsonRealPath();
	}
	/**
	 * 获取文件名称
	 * @auth wencst
	 * @date 2018年10月9日
	 * @return
	 */
	protected abstract String getFileName();
	
	protected String getContentAsString() {
		String content = null;
		try {
			File newFile = getNewFile();
			content = FileUtils.readFileToString(newFile);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
		return content;
	}
	
	protected <T> T getContentAsObject(Class<T> clazz) {
		T cmpNos = null;
		try {
			File newFile = getNewFile();
			String content = FileUtils.readFileToString(newFile);
			cmpNos = JSONObject.parseObject(content, clazz);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
		return cmpNos;
	}

	protected <T> List<T> getContentAsList(Class<T> clazz) {
		List<T> cmpNos = null;
		try {
			File newFile = getNewFile();
			String content = FileUtils.readFileToString(newFile);
			cmpNos = JSONObject.parseArray(content, clazz);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
		return cmpNos;
	}
	
	protected void writeIntoFile(Object object) {
		File file = getNewFile();
		String content = JSON.toJSONString(object);
		try {
			FileUtils.writeStringToFile(file, content, Charset.forName("UTF-8"));
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
	};
	/**
	 * 对已有文件增加数据内容
	 * @auth wencst
	 * @date 2018年10月9日
	 * @param tb
	 */
	protected abstract void addDataInto(TransferBean tb);
	/**
	 * 获取新文件
	 * @auth wencst
	 * @date 2018年10月9日
	 * @return
	 */
	private File getNewFile() {
        String folder = getDirName();
        File folderFile = new File(folder);
        if (!folderFile.exists()) {
        	folderFile.mkdirs();
        }
        File newFile = new File(folder, getFileName());
        return newFile;
	}

}
